package com.lsxh.maoqiuqiu.db;

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;

/**
 * @desc 数据库连接池,c3p0
 * @author buzc
 * @since 2010-07-02
 */
public class DatabasePool {
	private static Logger logger = Logger.getLogger(DatabasePool.class);
	private static DataSource unpooled;
	private static DataSource pooled;
	private static ComboPooledDataSource cpds;

	/**
	 * refuse
	 */
	private DatabasePool() {

	}

	static {
		// 1、可以通过工厂获取
		try {
			unpooled = DataSources.unpooledDataSource();
			pooled = DataSources.pooledDataSource(unpooled);
		} catch (SQLException e) {
			logger.error("init pool -- sql error");
		}
		// 2、可以直接实例化获取
		// cpds = new ComboPooledDataSource("mysql");
	}

	/**
	 * 
	 * 取出池内连接
	 */
	public static Connection getConnection() throws SQLException {
		Connection con = pooled.getConnection();
		// Connection con = cpds.getConnection();
		return con;
	}

}
